package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapplication.MyApplication;
import com.example.myapplication.R;
import com.example.myapplication.entity.User;
import com.example.myapplication.utils.SharedPreferencesDao;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUserName;
    private EditText etPassWord;
    private EditText etConfirmPassWord;
    private EditText etEmail;
    private Button btnRegister;
    private ImageView backView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bindView();
    }

    void bindView() {
        etUserName = findViewById(R.id.et_username);
        etPassWord = findViewById(R.id.et_passwd);
        etConfirmPassWord = findViewById(R.id.et_confirm_passwd);
        etEmail = findViewById(R.id.et_email);
        btnRegister = findViewById(R.id.btn_register);
        backView = findViewById(R.id.iv_back);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 注册
                String userName = etUserName.getText().toString();
                String passWord = etPassWord.getText().toString();
                String confirmPassWord = etConfirmPassWord.getText().toString();
                String email = etEmail.getText().toString();
                // 数据校验
                if (userName == null || userName.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入用户名", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
                if (passWord == null || passWord.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入密码", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                } else {
                    if (passWord.length() < 6) {
                        Toast res = Toast.makeText(getBaseContext(), "密码长度不能小于6位", Toast.LENGTH_SHORT);
                        res.show();
                        return;
                    }
                }
                if (!passWord.equals(confirmPassWord)) {
                    Toast res = Toast.makeText(getBaseContext(), "密码不一致", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
                if (email == null || email.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入邮箱", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }

                Log.i("Register", "userName:"+userName);
                Log.i("Register", "passWord:"+passWord);
                Log.i("Register", "confirmPassWord:"+confirmPassWord);
                Log.i("Register", "email:"+email);

                // 数据存储
                User user = new User();
                user.setUserName(userName);
                user.setPassword(passWord);
                user.setEmail(email);
                SharedPreferencesDao.saveUserInfo(getBaseContext(), user);

                // 注册成功
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("userName", user.getUserName());
                bundle.putString("password", user.getPassword());
                bundle.putString("email", user.getEmail());
                intent.putExtra("userInfo", bundle);
                setResult(1, intent);
                finish();
            }
        });

        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

}
